package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Khh;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Bm;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.organ.EjzhService;
import com.hnkywl.sts.service.organ.KhhService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:51
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/organ/ejzh")
public class EjzhAction extends SimpleActionSupport {
    private Ejzh ejzh;

    private Long nbgsId;
    private String qc;
    private String jc;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    private Long ejzhId;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    private Long khId;

    private EjzhService ejzhService;
    private BmService bmService;

    @Action("ejzhList")
    public void ejzhList() {
        this.renderJson(this.ejzhService.findEjzhList(nbgsId, qc, jc, page, rows));
    }

    @Action("ejzh_content")
    public String khhContent() {
        if (ejzhId != null) {
            ejzh = ejzhService.findEjzhById(ejzhId);
        } else {
            Bm bm = bmService.getEjzhbm();
            ejzh = new Ejzh();
            ejzh.setZh(bm.getBmz());
        }
        return SUCCESS;
    }

    @Action("save_ejzh")
    public void saveEjzh() {
        try {
            this.ejzhService.saveEjzh(ejzh);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加二级账户成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("update_ejzh")
    public void updateEjzh() {
        try {
            this.ejzhService.updateEjzh(ejzh);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改二级账户成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    @Action("delete_ejzh")
    public void deleteEjzh() {
        try {
            this.ejzhService.deleteEjzh(ejzhId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除二级账户成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!ejzhService.ajaxValidate(ejzhId, validateVal, validateField))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    @Action("findEjzhByKhId")
    public void findEjzhByKhId() {
        renderJson(ejzhService.findEjzhByKhId(khId,nbgsId));
    }

    @Action("findEjzhByKhAndNbgsId")
    public void findEjzhByKhAndNbgsId() {
        renderJson(ejzhService.findEjzhByKhAndNbgsId(khId,nbgsId));
    }


    public Ejzh getEjzh() {
        return ejzh;
    }

    public void setEjzh(Ejzh ejzh) {
        this.ejzh = ejzh;
    }

    public Long getNbgsId() {
        return nbgsId;
    }

    public void setNbgsId(Long nbgsId) {
        this.nbgsId = nbgsId;
    }

    public String getQc() {
        return qc;
    }

    public void setQc(String qc) {
        this.qc = qc;
    }

    public String getJc() {
        return jc;
    }

    public void setJc(String jc) {
        this.jc = jc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getEjzhId() {
        return ejzhId;
    }

    public void setEjzhId(Long ejzhId) {
        this.ejzhId = ejzhId;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    @Autowired
    public void setEjzhService(EjzhService ejzhService) {
        this.ejzhService = ejzhService;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
}
